<?php

namespace app\api\controller\wanlshop;

use app\api\model\wanlshop\Shop as WanlshopShop;
use app\common\controller\Api;
use app\common\model\Config;
use fast\Tree;
/**
 * WanlShop店铺接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getShopInfo','getshopconfig','apply','login'];
    protected $noNeedRight = ['*'];
	
	public function _initialize()
	{
	    parent::_initialize();
		$this->model = model('app\api\model\wanlshop\Shop');
	}

    public function storeInfo(){
        $get = $this->request->get();
        $store_id = $get['id'];
        $data =  model('app\api\model\wanlshop\Store')->find($store_id);

        $data['distance'] = distance($get['lat'],$get['lng'],$data['lat'],$data['lng']);
        $this->success('门店信息',$data);
    }

    public function storeCategory(){
        $store_id = $this->request->get('id');
        $list = model('app\api\model\wanlshop\Goods')->alias('a')
            ->join('wanlshop_category b','a.category_id=b.id')
            ->where('a.dianpu_id',$store_id)
            ->field('b.id,b.name,b.image')
            ->select();
        $this->success('店铺分类',$list);
    }
	
	/**
	 * 一次性获取店铺相关数据 1.0.8升级
	 *
	 * @ApiSummary  (WanlShop 一次性获取店铺相关数据)
	 * @ApiMethod   (GET)
	 *
	 * @param string $id 页面ID
	 */
	public function getShopInfo($id = null)
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		// 获取店铺信息
		$row = $this->model->get($id);
		if (!$row) {
		    $this->error(__('未找到此商家'));
		}
		// 获取商家类目
		$tree = Tree::instance();
		$tree->init(
			model('app\api\model\wanlshop\ShopSort')
				->where(['shop_id' => $row['id']])
				->field('id, pid, name, image')
				->order('weigh asc')
				->select()
		);
		$row['category'] = $tree->getTreeArray(0);
		// 查看是否被关注
		$row['isFollow'] = model('app\api\model\wanlshop\find\Follow')
			->where([
				'user_no' => $row['find_user']['user_no'], 
				'user_id' => $this->auth->id
			])
			->count();
		$row['isLive'] = model('app\api\model\wanlshop\Live')
			->where(['shop_id' => $row['id'], 'state' => 1])
			->field('id')
			->find();
		// 获取类目样式配置
		$shopConfig = model('app\api\model\wanlshop\ShopConfig')
			->where(['shop_id' => $row['id']])
			->find();
		$row['categoryStyle'] = (int)$shopConfig['category_style'];
		// 获取商家自定义页面
		$row['page'] = model('app\api\model\wanlshop\Page')
			->where([
				'shop_id' => $row['id'], 
				'type' => 'shop'
			])
			->field('id, name, page, item')
			->find();
		$this->success('返回成功', $row);
	}
	
	/**
	 * 商家入驻
	 *
	 * @ApiSummary  (WanlShop 店铺接口商家入驻)
	 * @ApiMethod   (POST)
	 */
	public function apply()
	{

		// $user_id = $this->auth->id;
		// var_dump($user_id);die;
		$user_id = 2;
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		$row = model('app\api\model\wanlshop\Auth')
			->where(['user_id' => $user_id])
			->order('id desc')
			->find();
		if ($this->request->isPost()) {
			$params = $this->request->post();
			$params['user_id'] = $user_id;
			$shop = \app\api\model\wanlshop\Shop::where(['user_id'=>$user_id,'state'=>$params['state']])->find();
			if($shop){
				$this->error('您已经成为此身份了');
			}

			$mobileshop = \app\api\model\wanlshop\Shop::where(['mobile'=>$params['fuze_molbile']])->find();
			if($mobileshop){
				$this->error('当前手机号已经被使用');
			}
			

			// if($row){
			// 	$row->save($params);
			// }else{
				model('app\api\model\wanlshop\Auth')->data($params)->save();
			// }
			$this->success('返回成功', $params);
		}
		$this->success('返回成功', $row);
	}

	 //获取商家配置
	 public function getshopconfig(){
		$config = Config::where('group','shopconfig')->order('id desc')->field('id,name,extend,value,type')->select();
		foreach($config as $k=>$v){
		 if($v['type'] == 'image'){
			 $config[$k]['value'] = cdnurl($v['value'],true);
		 }
		}
		$this->success('配置信息',$config);

	 }

	
	
}